package com.example.activemq.conf;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Queue;

@Configuration
public class BeanConfig {

    private final String BROKER_URL = "tcp://localhost:61616";
    private final String broker_username = "admin";
    private final String broker_password = "123456";

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(BROKER_URL);
        connectionFactory.setUserName(broker_username);
        connectionFactory.setPassword(broker_password);
        return connectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(connectionFactory());
        return template;
    }

/*    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrency("1-1");
        return factory;
    }*/

    @Bean(name = "jmsTopicListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsTopicListenerContainerFactory() {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory());
        //true为topic，false为queue
        factory.setPubSubDomain(true);
//        factory.setConcurrency("3-10");
        factory.setRecoveryInterval(1000L);
        return factory;
    }

    @Bean(name = "jmsQueueListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory() {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory());
        //true为topic，false为queue
        factory.setPubSubDomain(false);
//        factory.setConcurrency("3-10");
        factory.setRecoveryInterval(1000L);
        return factory;
    }

    @Bean(name="activeMQTopic")
    public ActiveMQTopic getActiveMQTopic(){
        ActiveMQTopic a=new ActiveMQTopic("activeMQTopic");//构造方法同时确定来队列消息的名字
        return a;
    }

    @Bean(name="activeMQQueue")
    public ActiveMQQueue activeMQQueue(){
        ActiveMQQueue a=new ActiveMQQueue("activeMQQueue");//构造方法同时确定来队列消息的名字
        return a;
    }

}
